import dialog from './dialog'
const isRequired = (validData, values) => {
  return Object.keys(values).every(v => {
    if (validData[v] && (!values[v] || !values[v].trim())) {
      dialog.showToast(validData[v])
      return false
    } else {
      return true
    }
  })
}
const isEmail = (email, show) => { // 先校验必填
  let mail = email && email.trim()
  if (!mail) {
    dialog.showToast('请填写邮箱')
  } else if (mail && !/^[a-z_0-9-.]+@([a-z_0-9-]+\.)+[a-z0-9]{2,8}$/i.test(mail)) {
    dialog.showToast(`邮箱格式不正确${show ? '（' + mail + '）' : ''}`)
    return false
  } else {
    return true
  }
}
const isPhone = (value, show) => { // 先校验必填
  let val = value && value.trim()
  if (!val) {
    dialog.showToast('请填写手机号')
  } else if (!/^(((\(\d{2,3}\))|(\d{3}-))?(1[2-9]\d{9}))$|^((001)[2-9]\d{9})$/.test(val)) {
    dialog.showToast(`手机号不正确${show ? '（' + val + '）' : ''}`)
    return false
  } else {
    return true
  }
}

export default {
  isRequired,
  isEmail,
  isPhone
}
